package com.dhcc.producter.advance.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.ExchangeBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.QueueBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitmqLazyQueueConfig {
	// 死信交换机
	public static final String LAZY_EXCHANGE_NAME = "my-lazy-exchange";

	// 正常交换机
	public static final String PROCESS_EXCHANGE_NAME = "order-exchange";

	// 死信队列
	public static final String LAZY_LETTER_QUEUE_NAME = "lazy-letter-queue";

	// 业务处理队列
	public static final String PROCESS_QUEUE_NAME = "order-queue";
	
	// 死信队列的routingKEY
	public static final String LAZY_ROUTING_KEY = "dead";

	// 业务处理队列的routingKEY
	public static final String PROCESS_ROUTING_KEY = "info";
	
	// 创建dlx
	@Bean
	public Exchange lazyExchange() {
		return ExchangeBuilder.directExchange(LAZY_EXCHANGE_NAME).durable(true).build();
	}

	// 创建死信队列
	@Bean
	public Queue lazyLetterQueue() {
		return QueueBuilder.durable(LAZY_LETTER_QUEUE_NAME).build();
	}

	// 绑定死信队列
	@Bean
	public Binding lazyBinding() {
		return BindingBuilder.bind(lazyLetterQueue()).to(lazyExchange()).with(LAZY_ROUTING_KEY).noargs();
	}

	// 创建正常交换机
	@Bean
	public Exchange orderExchange() {
		return ExchangeBuilder.directExchange(PROCESS_EXCHANGE_NAME).durable(true).build();
	}

	// 创建正常队列
	@Bean
	public Queue orderQueue() {
		return QueueBuilder.durable(PROCESS_QUEUE_NAME)
					.ttl(20000) // 设置队列存活时间 与死信队列不同的点one
					.deadLetterExchange(LAZY_EXCHANGE_NAME) // 绑定死信交换机
					.deadLetterRoutingKey(LAZY_ROUTING_KEY)
					.build();
	}
	
	// 绑定正常队列
	@Bean
	public Binding orderBinding() {
		return BindingBuilder.bind(orderQueue()).to(orderExchange()).with(PROCESS_ROUTING_KEY).noargs();
	}

}
